package de.moc.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.moc.shared.SurveyException;

@SuppressWarnings("serial")
public class AndroidServlet extends HttpServlet {

	private JSONMapper jm = new JSONMapper();

	private DataAccess da = new DataAccess();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		PrintWriter out = resp.getWriter();

		try {
			out.print(jm.OtoJ(da.getActiveSurvey()));
		} catch (SurveyException e) {
			out.print(jm.OtoJ(new DefaultSurvey()));
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String name = new BufferedReader(new InputStreamReader(
				req.getInputStream())).readLine();
		try {
			da.incChoiceOfActiveSurvey(name);
		} catch (SurveyException e) {
		}
	}
}
